package com.tankbattle.stage.body.tank;

import com.tankbattle.enums.DirectionEnum;
import com.tankbattle.stage.body.Bullet;
import com.tankbattle.utils.GetResource;

import java.applet.Applet;
import java.applet.AudioClip;
import java.util.concurrent.CopyOnWriteArrayList;



/**
 * @author 13797
 */
public class Player extends Tank implements GetResource {

    CopyOnWriteArrayList<Bullet> bulletPool;
    // limit time lag to fire
    int timeLag = 500;

    long extTime;

    public Player(int x, int y) {
        super(x, y);
        this.setSpeed(2);
        this.bulletPool = new CopyOnWriteArrayList<>();
        this.alive = true;
        this.moving = false;
        this.visible = false;
        this.life = 4;
        this.setFlag(0);
        bgm = Applet.newAudioClip(getResource("bgm/fire.wav"));
        this.extTime = System.currentTimeMillis();
    }

    long lastFire = 0;
    long lastestFire = 0;
    AudioClip bgm;

    public void fire() {

        if (!this.moving) {
            return;
        }
        lastestFire = System.currentTimeMillis();
        if (lastestFire - lastFire < timeLag) {
            return;
        } else {
            lastFire = lastestFire;
        }

        Bullet bullet = null;

        switch (this.getDirection()) {
            case UP:
                bullet = new Bullet(getX() + 12, getY() - 8, DirectionEnum.DOWN);
                break;
            case LEFT:
                bullet = new Bullet(getX() - 8, getY() + 12, DirectionEnum.LEFT);
                break;
            case DOWN:
                bullet = new Bullet(getX() + 12, getY() + 32, DirectionEnum.UP);
                break;
            case RIGHT:
                bullet = new Bullet(getX() + 32, getY() + 12, DirectionEnum.RIGHT);
                break;
            default:
                break;
        }
        bullet.setType(0);
        if (this.fastBullet) {
            bullet.setSpeed(22);
        }
        Thread fire = new Thread(bullet);
        fire.start();
        // play music
        bgm.play();
        this.bulletPool.add(bullet);

        bulletPool.removeIf(t -> !t.isAlive());
    }

    public void move(Boolean stop) {
        if (stop || !this.isMoving()) {
            return;
        }
        switch (this.getDirection()) {
            case UP:
                this.directionUp();
                break;
            case LEFT:
                this.directionLeft();
                break;
            case DOWN:
                this.directionDown();
                break;
            case RIGHT:
                this.directionRight();
                break;
            default:
                break;
        }
    }

    public void directionUp() {
        setY(getY() - getSpeed());
    }

    public void directionLeft() {
        setX(getX() - getSpeed());
    }

    public void directionDown() {
        setY(getY() + getSpeed());
    }

    public void directionRight() {
        setX(getX() + getSpeed());
    }

    public CopyOnWriteArrayList<Bullet> getBulletPool() {
        return bulletPool;
    }

    public long getExtTime() {
        return extTime;
    }

    public void setExtTime(long extTime) {
        this.extTime = extTime;
    }
}
